/**
 * IO函子,把有副作用的操作扔给调用者调用，函子还是保持纯函数
 */
const fs = require("fs");
const fp = require("lodash/fp");
class IO {
  static of(value) {
    return new IO(() => {
      return value;
    });
  }

  constructor(fn) {
    this._value = fn;
  }

  map(fn) {
    return new IO(fp.flowRight(fn, this._value));
  }
}

const readFile = (fileName) => {
  return new IO(() => {
    return fs.readFileSync(fileName,"utf-8");
  });
};
const print = (x) => {
  return new IO(() => {
    console.log("内部", x);
    return x;
  });
};

const cat = fp.flowRight(print, readFile);
console.log(cat);
const f = cat("package.json");
console.log(f._value()._value());
